/**
 * 管理员社交管理页面
 * @author 韩睿倩
 */

<template>
  <div class="booklet-list-container">
    <div class="booklet-list-inner" style="margin-left: 300px; margin-right: 300px">
      <el-table :data="tableData" style="width: 100%" :default-sort="{ prop: 'date', order: 'descending' }">
        <el-table-column prop="createDate" label="日期" sortable width="auto">
        </el-table-column>
        <el-table-column prop="bookletName" label="单词本名称" sortable width="auto">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click="handleBookletDetail(scope.$index, tableData)" type="text" size="small">
              查看详情
            </el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click="handleHas(scope.$index, tableData)" type="text" size="small">
              收藏
            </el-button>
            <el-button @click="handleDelete(scope.$index, tableData)" type="danger" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tabListPage">
        <el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="PageSize"
          layout="prev, pager, next, jumper">
        </el-pagination>
      </div>
    </div>

    <div>
      <el-dialog :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
        <el-descriptions>
          <el-descriptions-item label="单词本名称">{{
              bookletDetail.bookletName
          }}</el-descriptions-item>
          <el-descriptions-item label="单词本作者">{{
              bookletDetail.bookletAuthor
          }}</el-descriptions-item>
          <el-descriptions-item label="单词本描述">{{
              bookletDetail.bookletIntro
          }}</el-descriptions-item>
          <el-descriptions-item label="单词总数">
            <el-tag size="small">{{ bookletDetail.bookletEntryCount }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">{{
              bookletDetail.bookletCreateDate
          }}</el-descriptions-item>
        </el-descriptions>

        <el-table :data="currentEntries.list" style="width: 100%">
          <el-table-column prop="wordSpell" label="单词拼写" sortable width="auto"></el-table-column>
        </el-table>
        <el-pagination @current-change="handleEntryCurrChange" :current-page="currentEntries.page"
          :page-size="currentEntries.pageSize" layout="total, prev, pager, next, jumper">
        </el-pagination>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import {
  getShareBooklets,
  getShareBookletDetail,
  hasShareBook,
  deleteShareBooklet
} from "@/api/user/shareBookletApi.js";

import { getBookletEntries } from "@/api/user/bookletApi.js";

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      totalCount: 1,
      PageSize: 10,
      dialogVisible: false,
      bookletFormatVisible: false,
      bookletDetail: {},
      currentBookletId: NaN,
      currentEntries: {
        page: 1,
        pageSize: 10,
      },
      bookletForm: {
        name: "",
        introduction: "",
      },
    };
  },
  computed: {
    userId() {
      return this.$store.getters.userId;
    },
  },
  methods: {
    getData(page, pageSize) {
      getShareBooklets({
        page: page,
        pageSize: pageSize,
      })
        .then((res) => {
          console.log(res);
          // 这里处理后端返回的结果
          this.tableData = res.data.list;
          this.currentPage = res.data.page;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    getBookletDetai(index, tableData) {
      getShareBookletDetail(tableData[index].bookletId)
        .then((res) => {
          console.log(res);
          this.bookletDetail = res.data;
        })
        .catch();
    },

    getEntries(index, tableData, pageNum, pageSize) {
      getBookletEntries(tableData[index].bookletId, {
        page: pageNum,
        pageSize: pageSize,
      })
        .then((res) => {
          this.currentEntries = res.data;
        })
        .catch();
    },

    // 显示第几页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData(val, this.PageSize);
    },

    handleBookletDetail(index, tableData) {
      this.dialogVisible = true;
      this.currentBookletId = tableData[index].bookletId;

      this.getBookletDetai(index, tableData);

      this.getEntries(
        index,
        tableData,
        this.currentEntries.page,
        this.currentEntries.pageSize
      );
    },

    handleHas(index, tableData) {
      let bookletId = tableData[index].bookletId;
      hasShareBook(bookletId, { userId: this.$store.getters.userId }).then(
        (res) => {
          if (res.resultCode == 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
          } else {
            this.$notify({
              title: "失败",
              message: res.message,
              type: "warning",
            });
          }
        }
      );
    },

    handleClose(done) {
      done();
    },

    handleEntryCurrChange(val) {
      getBookletEntries(this.currentBookletId, {
        page: val,
        pageSize: this.currentEntries.pageSize,
      })
        .then((res) => {
          this.currentEntries = res.data;
        })
        .catch();
    },

    handleDelete(index, tableData) {


      console.log("okkkkkkk")
      //   deleteShareBooklet(tableData[index].bookletId).then((res)=>{
      //     console.log(res)
      //   }).catch((err)=>{
      //     console.log(err)
      //   })
      //   console.log("okkk11111kkkk")
      // }
      // 二次确认
      this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 发送删除请求
          console.log(tableData[index].bookletId)
          deleteShareBooklet(tableData[index].bookletId)
            .then((res) => {
              console.log("delete into ")
              this.$notify({
                title: "成功",
                message: res.message,
                type: (res.resultCode === 200 ? "success" : "warning"),
              });
            })
            .catch();
          this.getData(this.currentPage, this.PageSize);
        })
        .catch(() => { })
        .then(() => {
          this.dialogVisible = false;
        });
    },
  },

  created: function () {
    this.getData(this.currentPage, this.PageSize);
  },
}
</script>

<style lang="scss" scoped>
.booklet-list-container {
  margin-top: 80px;
}

.booket-name {
  color: aqua;
}
</style>